<script>


export default {
  name: 'loginForm',
  data() {
    return {
      // 登录的参数
      loginForm: {
        account: 'demo',
        password: 'hm#qd@23!',
        code: '',
        key: ''
      },
      /* 表单校验 */
      loginRules: {
        account: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入验证码', trigger: 'blur' }
        ]

      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      codeImage: ''
    }
  },
  methods: {
    showPwd() {
      this.passwordType === 'password' ? this.passwordType = '' : this.passwordType = 'password'
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    /*
    * 获取登录验证码的回调函数
    *
    *  */
    async getCodeLogin() {
      try {
        this.loginForm.key = Date.now()
        this.codeImage = await this.$store.dispatch('userStore/getLoginCodeAction', this.loginForm.key)
      } catch (error) {
        this.$message.error(error.message)
      }
    },
    /*
    * 点击登录按钮的回调函数
    *  */
    async handleLogin() {
      const validate = await this.$refs.formRef.validate().catch(error => error)
      if (!validate) return
      try {
        this.loading = true
        await this.$store.dispatch('userStore/userLoginAction', this.loginForm)
        this.$router.push(this.redirect || '/')
        this.$message.success('登录成功')
      } catch (error) {
        this.$message.error(error.message)
        this.getCodeLogin()
      } finally {
        this.loading = false
      }
    },
    /*
    *为文档绑定键盘事件
    *
    *  */
    bindKeyUp() {
      document.addEventListener('keyup', ({ keyCode }) => {
        if (keyCode === 13) {
          this.handleLogin()
        }
      })
    }
  },

  mounted() {
    this.getCodeLogin()
    this.bindKeyUp()
  },
  beforeDestroy() {
    document.removeEventListener('keyup', this.handleLogin)
  }
}
</script>

<template>
  <el-form ref="formRef" :model="loginForm" :rules="loginRules" class="login-form">

    <el-form-item prop="account">
        <span class="svg-container">
          <svg-icon icon-class="user"/>
        </span>
      <el-input ref="username" v-model="loginForm.account" placeholder="请输入用户名"/>
    </el-form-item>

    <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password"/>
        </span>
      <el-input :key="passwordType" ref="password" v-model="loginForm.password" :type="passwordType" placeholder="请输入密码" @keyup.enter.native="handleLogin"/>
      <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"/>
        </span>
    </el-form-item>

    <el-form-item prop="code">
        <span class="svg-container">
          <svg-icon icon-class="code"/>
        </span>
      <el-input v-model="loginForm.code" placeholder="请输入验证码"/>
      <span>
        <img :src="codeImage" class="code" @click="getCodeLogin">
      </span>
    </el-form-item>

    <el-button :loading="loading" type="primary" @click.native.prevent="handleLogin">登录</el-button>

  </el-form>
</template>

<style scoped lang="scss">
$dark_gray: #889aa4;
$light_gray: #eee;

.login-form {
  position: relative;
  width: 520px;
  max-width: 100%;
  padding: 10px 25px;
  margin: 0 auto;
  overflow: hidden;
}

.svg-container {
  padding: 6px 5px 6px 15px;
  color: $dark_gray;
  vertical-align: middle;
  width: 40px;
  display: inline-block;
}

.show-pwd {
  position: absolute;
  right: 10px;
  top: 7px;
  font-size: 16px;
  color: $dark_gray;
  cursor: pointer;
  user-select: none;
}

.code {
  width: 100px;
  position: absolute;
  top: 50%;
  right: 10px;
  transform: translateY(-50%);
  cursor: pointer;

}

.el-button {
  width: 100%;
  height: 50px;
  font-size: 17px;
  margin-top: 10px;
  font-weight: 600;
}

</style>
